package fy.form.listen.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import fy.config.FyConfig;
import fy.form.listen.bean.UserBean;
import fy.util.DBUtil;

public class MemberDao {

	public static UserBean queryMemberIDByCode(String code) {
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		UserBean userBean = null;
		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());

			st = conn
					.prepareStatement("select m.id, m.name from v3x_org_member m where m.code = ?");

			st.setString(1, code);

			rs = st.executeQuery();

			if (rs.next()) {
				userBean = new UserBean();
				userBean.setId(rs.getLong(1));
				userBean.setName(rs.getString(2));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, st, conn);
		}
		return userBean;
	}

	public static List<UserBean> queryMemberIDByCode(String[] codes) {
		if (codes == null || codes.length == 0) {
			return null;
		}
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		UserBean userBean = null;
		List<UserBean> list = null;
		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());

			StringBuilder sb = new StringBuilder()
					.append("select m.id, m.name from v3x_org_member m where ( m.code = ? ");
			for (int i = 1, length = codes.length; i < length; i++) {
				sb.append(" or m.code = ? ");
			}
			sb.append(" ) and m.code is not null ");
			st = conn.prepareStatement(sb.toString());
			for (int i = 0, length = codes.length; i < length; i++) {
				st.setString(i + 1, codes[i]);
			}
			rs = st.executeQuery();
			list = new ArrayList<UserBean>();
			while (rs.next()) {
				userBean = new UserBean();
				userBean.setId(rs.getLong(1));
				userBean.setName(rs.getString(2));
				list.add(userBean);
				userBean = null;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, st, conn);
		}
		return list;
	}
}
